package com.twu.biblioteca;

import java.util.ArrayList;

/**
 * Created by theyes on 17/01/2014.
 */
public abstract class ListMenu {

    public MockItemDataBase DB;

    public ListMenu(MockItemDataBase db)
    {
        DB = db;

    }

    public void startMenu()
    {
        UserInputHandler userInputHandle = new UserInputHandler();

        printUserInstruction();

        listAll();

        int userChoice = userInputHandle.readInputInt();

        processUserInput(userChoice);

    }


    public void listAll()
    {
        ArrayList<LibraryItem> listOfLibraryItems =  DB.getAllItems();

        System.out.println("0) Return to the Main Menu.");

        for(int i =0; i < listOfLibraryItems.size(); i++ )
        {
            System.out.println(i + 1 + ") " + listOfLibraryItems.get(i).toString());
        }

    }

    public abstract void printUserInstruction();


    public void processUserInput(int userChoice)
    {
        if(userChoice == 0)
        {
            return;
        }
        else if(userChoice-1 >= DB.getAllItems().size())
        {
            System.out.println("Invalid choice item doesn't exist.");
        }
        else
        {
            if(DB.attemptToCheckOutItem(userChoice - 1))
            {
                System.out.println("Thank you! Enjoy the " + DB.getAllItems().get(userChoice - 1).toString());

            }
            else
            {
                System.out.println("Sorry you can't check out that item as we don't have any copies left.");
            }
        }
    }
}
